package com.qububu.play.common.control;

import java.util.Collection;

import javax.annotation.Resource;
import javax.servlet.ServletContext;

import org.directwebremoting.Browser;
import org.directwebremoting.ScriptBuffer;
import org.directwebremoting.ScriptSession;
import org.directwebremoting.ScriptSessionFilter;
import org.directwebremoting.WebContextFactory;
import org.directwebremoting.annotations.RemoteMethod;
import org.directwebremoting.annotations.RemoteProxy;
import org.springframework.stereotype.Service;
import org.springframework.web.context.ServletContextAware;

import com.qububu.play.common.dto.ActionTalkoverDto;
import com.qububu.play.common.dto.TalkBaseMessage;
import com.qububu.play.common.service.TalkService;
import com.qububu.utils.DateUtil;

@Service
@RemoteProxy(name="dwrSend") 
public class DwrSendMessage implements ServletContextAware{
	
	@Resource
	private TalkService talkService;
	
	private ServletContext cx;
	public void setServletContext(ServletContext cx) {
		this.cx = cx;
	}
	
	@RemoteMethod
	public void sendTargetUserMessage(ActionTalkoverDto message){
		
		final ActionTalkoverDto msg  = createTalkMessage(message);
		Browser.withAllSessionsFiltered(new ScriptSessionFilter() {
			
			public boolean match(ScriptSession session) {
				if(session.getAttribute("userId") == null)
					return false;
				else
					return (session.getAttribute("userId").equals(msg.getTalkId()));
			}
		},new Runnable() {
			
			private ScriptBuffer sb  = new ScriptBuffer();
			public void run() {
				sb.appendCall("showMessage", msg);
				Collection<ScriptSession> sessions = Browser.getTargetSessions();
				
				for(ScriptSession session:sessions){
					session.addScript(sb);
				}
			}
		});
		
	}
	@RemoteMethod
	public void sendAllUsersMessage(ActionTalkoverDto message){
		
		final ActionTalkoverDto msg  = createTalkMessage(message);
		Browser.withAllSessions(new Runnable() {
			private ScriptBuffer sb  = new  ScriptBuffer();
			
			public void run() {
				sb.appendCall("showMessage", msg);
				Collection<ScriptSession> sessions = WebContextFactory.get().getScriptSessionsByPage(cx.getContextPath()+"/play/talk/chat");
				for(ScriptSession session:sessions){
					session.addScript(sb);
				}
			}
		});
		
		talkService.doSaveTalkMessage(msg);
	}
	
	/*public final String createNowDate(){
		SimpleDateFormat sf = new SimpleDateFormat("HH:mm:ss");
		String time = sf.format(new Date());
		
		return time;
	}*/
	
	public final ActionTalkoverDto createTalkMessage(ActionTalkoverDto message){
		TalkBaseMessage talkBaseMessage = (TalkBaseMessage) WebContextFactory.get().getScriptSession().getAttribute("talkBaseMessage");
		ActionTalkoverDto msg = new ActionTalkoverDto();
		msg.setContent(message.getContent());
		msg.setTime(DateUtil.convertCurrentDTTMtoInt());
		msg.setAuthorId(talkBaseMessage.getUserId());
		msg.setAuthorName(talkBaseMessage.getNickName());
		msg.setActionId(talkBaseMessage.getActionId());
		msg.setType(talkBaseMessage.getActionType());
		
		return msg;
	}
	
	@RemoteMethod
	public void Test(String str){
		ScriptSession session = WebContextFactory.get().getScriptSession();
		System.out.print(session.getAttribute("userName")+"=========="+str);
	}
	
}
